import {messages} from "share/common";
import React, { Component } from 'react';
import constant from "../constant";
import SettingService from "../setting-service";
import { Button, Form, message, Radio } from "antd";
import Selector from "components/selector";

class ApyTypeRule extends Component {

  state = {
    saveLoading: false,
    paymentMethodCategory: null
  };

  componentDidMount() {
    const { record } = this.props.params;
    if (record.id) {
      this.setState({ paymentMethodCategory: record.paymentMethodCategory })
    } else {
      const { accountType } = this.props;
      let isAli = accountType === 'ALIPAY_LOGON_ID';
      isAli && this.setState({ paymentMethodCategory: 'ONLINE_PAYMENT' })
    }
  }

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        this.setState({ saveLoading: true });
        const { record, id } = this.props.params, { close, accountType } = this.props;
        value.bankAccountId = id;
        value.accountType = accountType;
        SettingService.saveApyType(Object.assign(record, value)).then(() => {
          this.setState({ saveLoading: false }, () => {
            message.success(messages('common.operate.success')/*操作成功*/);
            close(true);
          });
        }).catch(e => {
          message.error(e.response.data.message);
          this.setState({ saveLoading: false });
        })
      }
    })
  };

  handlePaymentMethodCategory = value => {
    this.setState({ paymentMethodCategory: value });
    this.props.form.setFieldsValue({ paymentMethodId: undefined });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { saveLoading, paymentMethodCategory } = this.state;
    const { formItemLayout } = constant;
    const { close, accountType } = this.props, { record, id } = this.props.params;
    let isAli = accountType === 'ALIPAY_LOGON_ID';
    return(
      <div>
        <Form className={'new-form'}>
          <Form.Item
            label={messages('finance-1.key26')/*付款方式类型*/}
            {...formItemLayout}
          >
            {getFieldDecorator("paymentMethodCategory", {
              rules: [{ required: true, message: messages('common.select')/*请选择*/ }],
              initialValue: Object.keys(record).length > 0 ?
                { key: record.paymentMethodCategory, label: record.paymentMethodCategoryName } : (
                  isAli ? { key: "ONLINE_PAYMENT", label: "线上" } : undefined
                )
            })(
              <Selector
                onChange={this.handlePaymentMethodCategory}
                type='value_list'
                itemMap
                disabled={isAli}
                params={{ type: 2105 }}
              />
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-1.key311')/*付款方式*/}
            {...formItemLayout}
          >
            {getFieldDecorator("paymentMethodId", {
              rules: [{ required: true, message: messages('common.select')/*请选择*/ }],
              initialValue: Object.keys(record).length > 0 ? { key: record.paymentMethodId, label: record.description } : undefined
            })(
              <Selector
                disabled={!paymentMethodCategory}
                type={'apy_type_list'}
                itemMap
                params={{
                  companyBankId: id,
                  type: paymentMethodCategory,
                  companyPaymentId: record.id,
                  paymentMethodId: record.paymentMethodId
                }}
              />
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-1.key359')/*设为默认*/}
            {...formItemLayout}
          >
            {getFieldDecorator("defaults", {
              rules: [{ required: true }],
              initialValue: record.hasOwnProperty('isDefault') ? record.isDefault : false
            })(
              <Radio.Group options={[
                { label: messages('common.yes')/*是*/, value: true },
                { label: messages('common.no')/*否*/, value: false }
              ]}/>
            )}
          </Form.Item>
          <div className='footer-operate'>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
            <Button onClick={() => close()}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

export default Form.create()(ApyTypeRule)
